package com.xh.jf_blog.controller.front;

import com.xh.jf_blog.bean.*;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.common.util.SessionInfo;
import com.xh.jf_blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/2/20/020
 */
@Controller("article")
@RequestMapping("/article")
@ResponseBody
public class ArticleController extends SessionInfo {
    @Autowired
    private AnthologyService anthologyService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/anthology/list")
    public R anthologyList() {
        return R.ok(anthologyService.getSelect(getVipId()));
    }

    @PostMapping("/anthology/update")
    public R anthologyUpdate(Anthology anthology) {
        anthology.setUserinfoId(getVipId());
        anthologyService.saveOrUpdate(anthology);
        return R.ok();
    }

    @DeleteMapping("/anthology/delete/{anthologyId}")
    public R anthologyDelete(@PathVariable int anthologyId) {
        anthologyService.delete(anthologyId);
        return R.ok();
    }

    @GetMapping("/list/{anthologyId}")
    public R articleList(@PathVariable int anthologyId) {
        return R.ok(articleService.getListByAnthologyId(anthologyId));
    }

    @PostMapping("/update")
    public R articleUpdate(Article article) {
        article.setUserinfoId(getVipId());
        articleService.saveOrUpdate(article);
        return R.ok();
    }

    @GetMapping("/info/{articleId}")
    public R articleInfo(@PathVariable int articleId) {
        return R.ok(articleService.getById(articleId));
    }

    @DeleteMapping("/delete/{articleId}")
    public R articleDelete(@PathVariable int articleId) {
        articleService.deleteById(articleId);
        return R.ok();
    }

    @GetMapping("/tag/list")
    public R tagList() {
        return R.ok(tagService.getListByUserinfoId(getVipId()));
    }

    @PostMapping("/tag/update")
    public R tagUpdate(Tag tag) {
        tag.setUserinfoId(getVipId());
        tagService.saveOrUpdate(tag);
        return R.ok(tag);
    }

    @DeleteMapping("/tag/delete/{tagId}")
    public R tagDelete(@PathVariable int tagId) {
        tagService.deleteById(tagId);
        return R.ok();
    }

    @PostMapping("/comment/save")
    public R saveComment(Comment comment) {
        comment.setUserinfoId(getVipId());
        commentService.saveOrUpdate(comment);
        return R.ok();
    }

    @DeleteMapping("/comment/delete/{commentId}")
    public R deleteComment(@PathVariable int commentId) {
        commentService.deleteById(commentId);
        return R.ok();
    }

    @PostMapping("/user/update")
    public R updateUser(Userinfo userinfo) {
        userinfoService.saveOrUpdate(userinfo);
        return R.ok();
    }

    @GetMapping("/user/check/password")
    public R checkPassword(Userinfo userinfo) {
        return userinfoService.checkPassword(userinfo);
    }

    @PostMapping("/user/password")
    public R updatePassword(Userinfo userinfo) {
        userinfoService.saveOrUpdate(userinfo);
        return R.ok();
    }


}
